<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class GameLesson extends Model
{
    //
    protected $connection = 'api';
    protected $table = 'game_lesson';
    protected $gameActiveTable = 'game_truth_or_dare_logs';
    public $timestamps = false;
    public $fillable = ['type','title','desc','content','status','create_timestamp'];
    private $fields = ['lesson_id','type','title','desc','content','status','create_timestamp','status'];

    public function getList($input = []){
        $page = $input['page']??1;
        $limit = $input['limit']??10;
        $title = $input['title']??'';
        $type = $input['type']??'';
        $status = $input['status']??0;

        $where = [
            'status' => 0,
        ];
        if ($type != '') $where[] = ['type',$type];
        if ($status) $where[] = ['status',$status];
        if ($title != '') $where[] = ['title','like','%'.$title.'%'];

        return self::where($where)->paginate($limit, $this->fields, 'page', $page);
    }
    public function createLesson($input){
        return curl_request(getKkaudioApiUrl() . '/inner/game/lesson/create', $input, 'json', 'POST');
    }
    public function updateLesson($input){
        return curl_request(getKkaudioApiUrl() . '/inner/game/lesson/update', $input, 'json', 'POST');
    }
    public function gameActiveList($input){
        $page = $input['page']??1;
        $limit = $input['limit']??10;

        $data = DB::connection('api')->table($this->gameActiveTable)->paginate($limit, ['*'], 'page', $page);
        $dataArr = json_decode(json_encode($data->items()),true);

        $sponsor_user_id = array_column($dataArr, 'sponsor_user_id');
        $player_user_id = array_column($dataArr, 'player_user_id');
        $roomids = array_unique(array_column($dataArr, 'room_id'));
        $user_ids = array_unique(array_merge($sponsor_user_id,$player_user_id));

        $room = new Room();
        $roomData = $room->getRoomListByIds($roomids);
        $roomDataArr = array_column($roomData ,null ,'room_id');

        $user = new Users();
        $userData = $user->getUserInfoByIds($user_ids);
        $userDataArr = array_column($userData ,null ,'user_id');
        foreach ($data as $k => $row){
            if ($userDataArr[$row->sponsor_user_id]){
                $data[$k]->sponsor_user_uuid = $userDataArr[$row->sponsor_user_id]->uuid;
                $data[$k]->sponsor_user_nickname = $userDataArr[$row->sponsor_user_id]->nickname;
                $data[$k]->player_user_uuid = $userDataArr[$row->player_user_id]->uuid;
                $data[$k]->player_user_nickname = $userDataArr[$row->player_user_id]->nickname;
            }
            if ($roomDataArr[$row->room_id]){
                $data[$k]->room_uid = $roomDataArr[$row->room_id]->room_uid;
                $data[$k]->room_name = $roomDataArr[$row->room_id]->name;
            }
        }
        return $data;
    }
}
